package de.dis2011.data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class article extends productgroup
{
    private int articleid = -1;
//    private int productgroupid;
    private String articlename;
    private double preis;

    public int getArticleid()
    {
        return articleid;
    }

    public void setArticleid(int articleid)
    {
        this.articleid = articleid;
    }

//    public int getProductgroupid()
//    {
//        return productgroupid;
//    }
//
//    public void setProductgroupid(int productgroupid)
//    {
//        this.productgroupid = productgroupid;
//    }

    public String getArticlename()
    {
        return articlename;
    }

    public void setArticlename(String articlename)
    {
        this.articlename = articlename;
    }

    public double getPreis()
    {
        return preis;
    }

    public void setPreis(double preis)
    {
        this.preis = preis;
    }

    /**
     * Lädt einen Artikel aus der Datenbank
     * 
     * @param id
     * 
     * @return Artikel-Instanz
     */
    public static article load(int id)
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM articleid WHERE articleid = ?";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);
            pstmt.setInt(1, id);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            if (rs.next())
            {
                article ts = new article();
                ts.setArticleid(rs.getInt("articleid"));
                ts.setArticlename(rs.getString("name"));
                ts.setPreis(rs.getDouble("preis"));
                ts.setProductgroupid(rs.getInt("productgroupid"));
                
                System.out.println("ID: " + ts.getArticleid() + " Name: " + ts.getArticlename() + " Preis: " + ts.getPreis());

                // rs.close();
                // pstmt.close();
                // return ts;
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static article loadAll()
    {
        try
        {
            // Hole Verbindung
            Connection con = DB2ConnectionManager.getInstance().getConnection();

            // Erzeuge Anfrage
            String selectSQL = "SELECT * FROM articleid";
            PreparedStatement pstmt = con.prepareStatement(selectSQL);

            // Führe Anfrage aus
            ResultSet rs = pstmt.executeQuery();
            while (rs.next())
            {
                article ts = new article();
                ts.setArticleid(rs.getInt("articleid"));
                ts.setArticlename(rs.getString("name"));
                ts.setPreis(rs.getDouble("preis"));
                ts.setProductgroupid(rs.getInt("productgroupid"));

                System.out.println(ts.getArticleid() + " " + ts.getProductgroupid() + " " + ts.getArticlename() + " "
                        + ts.getPreis());

                // rs.close();
                // pstmt.close();
                // return ts;
            }
            rs.close();
            pstmt.close();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

}
